#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace ICSharpCode::SharpZipLib::Zip;
using namespace System::IO;


namespace Zipacz {

	/// <summary>
	/// Summary for SettingsForm
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class SettingsForm : public System::Windows::Forms::Form
	{
	public:
		SettingsForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

		bool ShowForm(String^% _xml)
		{
			if (_xml=="")
			{
				textBox1->Text = "";
				textBox2->Text = "";
			}
			else
			{
				int indx = _xml->IndexOf(';');
				textBox1->Text = _xml->Substring(0, indx);
				textBox2->Text = _xml->Substring(indx+1, _xml->Length-indx-1);
				label3->Text   = getZipFileName(textBox1->Text, textBox2->Text);
			}

			if (ShowDialog()==Windows::Forms::DialogResult::OK)
			{
				_xml = textBox1->Text+";"+textBox2->Text;
				return true;
			}

			return false;
		}
		
		bool static Pakuj(String^ _xml)
		{
			int indx = _xml->IndexOf(';');
			String^ dir  = _xml->Substring(0, indx);
			String^ file = _xml->Substring(indx+1, _xml->Length-indx-1);					

			if (!Directory::Exists(dir) || !Directory::Exists(file))return false;

			file = getZipFileName(dir, file);

			PakujKatalog(dir, file);

			return true;
		}

		void static PakujKatalog(String^ _dir, String^ _file)
		{
			ZipOutputStream^ wyjscie = gcnew ZipOutputStream( File::Create(_file));
			DirectoryInfo^ d         = gcnew DirectoryInfo(_dir);
			kompresuj(_dir, wyjscie, d->Root->ToString());
			wyjscie->Flush();
			wyjscie->Close();
		}

		static void kompresuj(String^ wejscie, ZipOutputStream^ wyjscie, String^ korzen)
		{
			wyjscie->SetLevel(5);			   

			array<String^>^katalogi = Directory::GetDirectories(wejscie);
			array<String^>^pliki    = Directory::GetFiles(wejscie);
			     

			if(pliki->Length==0 && wejscie!=korzen)
			{
				String^ katalog = wejscie->Replace(korzen, "");
				ZipEntry^ ze    = gcnew ZipEntry(katalog + "/");
				wyjscie->PutNextEntry(ze);
			}

			for each(String^ katalog in katalogi)
			{
				kompresuj(katalog, wyjscie, korzen);
			}

			for each(String^ plik in pliki)
			{
				FileStream^ strumien = File::OpenRead(plik);				
				array<unsigned char> ^bufor = gcnew array<unsigned char>((int)strumien->Length);// Byte[strumien->Length];				
				strumien->Read(bufor, 0, bufor->Length);				
				strumien->Close();
				String^ nowyPlik = plik->Replace(korzen, "");
				ZipEntry^ ze     = gcnew ZipEntry(nowyPlik);
				wyjscie->PutNextEntry(ze);
				wyjscie->Write(bufor, 0, bufor->Length);
			}

		}

	private:
		static String^ getZipFileName(String^ _dir_in, String^ _dir_out)
		{
			if (!Directory::Exists(_dir_in) || !Directory::Exists(_dir_out))return "";

			DirectoryInfo^ d = gcnew DirectoryInfo(_dir_in);
			DateTime data = DateTime::Now;

			return _dir_out+"\\"+d->Name+"_"+data.Year.ToString()+"_"+data.Month.ToString()+"_"+data.Day.ToString()+".zip";
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~SettingsForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Button^  button3;

	private: System::Windows::Forms::FolderBrowserDialog^  DirDialog;
	private: System::Windows::Forms::Button^  button4;
private: System::Windows::Forms::Label^  label3;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->DirDialog = (gcnew System::Windows::Forms::FolderBrowserDialog());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(12, 30);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(241, 20);
			this->textBox1->TabIndex = 0;
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(12, 80);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(241, 20);
			this->textBox2->TabIndex = 1;
			// 
			// button1
			// 
			this->button1->DialogResult = System::Windows::Forms::DialogResult::OK;
			this->button1->Location = System::Drawing::Point(41, 130);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 2;
			this->button1->Text = L"Ok";
			this->button1->UseVisualStyleBackColor = true;
			// 
			// button2
			// 
			this->button2->DialogResult = System::Windows::Forms::DialogResult::Cancel;
			this->button2->Location = System::Drawing::Point(159, 130);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(75, 23);
			this->button2->TabIndex = 3;
			this->button2->Text = L"Anuluj";
			this->button2->UseVisualStyleBackColor = true;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(9, 14);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(118, 13);
			this->label1->TabIndex = 4;
			this->label1->Text = L"Katalog do spakowania";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(9, 64);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(155, 13);
			this->label2->TabIndex = 5;
			this->label2->Text = L"Katalog utworzonego archiwum";
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(259, 30);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(24, 23);
			this->button3->TabIndex = 6;
			this->button3->Text = L"...";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &SettingsForm::button3_Click);
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(259, 78);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(24, 23);
			this->button4->TabIndex = 7;
			this->button4->Text = L"...";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &SettingsForm::button4_Click_1);
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(12, 103);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(16, 13);
			this->label3->TabIndex = 8;
			this->label3->Text = L"...";
			// 
			// SettingsForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(297, 165);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
			this->Name = L"SettingsForm";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"Ustawienia Zipacza";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
				 if (DirDialog->ShowDialog()==Windows::Forms::DialogResult::OK)
				 {
					 textBox1->Text = DirDialog->SelectedPath;
					 label3->Text   = getZipFileName(textBox1->Text, textBox2->Text);
				 }
			 }
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {			 
		 }
private: System::Void button4_Click_1(System::Object^  sender, System::EventArgs^  e) {
			 if (DirDialog->ShowDialog()==Windows::Forms::DialogResult::OK)
			 {
				textBox2->Text = DirDialog->SelectedPath;
				label3->Text   = getZipFileName(textBox1->Text, textBox2->Text);
			 }
		 }
};
}
